package com.cskaoyan.service;

import com.cskaoyan.bean.Coupon;
import com.cskaoyan.bean.CouponExample;
import com.cskaoyan.bean.CouponUser;
import com.cskaoyan.bean.CouponUserExample;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.cskaoyan.vo.CouponUserVO;

import com.cskaoyan.vo.CouponVO;
import com.github.pagehelper.PageHelper;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CouponServiceImpl implements CouponService{
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;
    @Override
    public CouponVO queryCoupon(Integer page, Integer limit, String name, Integer type, Integer status, String sort, String order) {

        PageHelper.startPage(page, limit);

        CouponExample couponExample = new CouponExample();

         couponExample.setOrderByClause(sort + " " + order);

        CouponExample.Criteria criteria = couponExample.createCriteria();


        if (name != null && !"".equals(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (type!= null && !"".equals(type)) {
            criteria.andIdEqualTo(type);
        }
        if (status != null && !"".equals(status)) {
           criteria.andStatusEqualTo(status);
        }
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);

        PageInfo<Coupon> couponPageInfo = new PageInfo<>(coupons);

        long total = couponPageInfo.getTotal();

        CouponVO couponVO = new CouponVO();

        couponVO.setTotal(total);

        couponVO.setItems(coupons);

        return couponVO ;
    }

    @Override
    public CouponUserVO queryUser(Integer page, Integer limit, Integer couponId, Integer userId, Integer status, String sort, String order) {


        PageHelper.startPage(page, limit);

        CouponUserExample couponUserExample = new CouponUserExample();

        couponUserExample.setOrderByClause(sort + " " + order);

       couponUserExample.createCriteria().andCouponIdEqualTo(couponId).andDeletedEqualTo(false);

        List<CouponUser> couponUsers = couponUserMapper.selectByExample(couponUserExample);

        PageInfo<CouponUser> couponUserPageInfo = new PageInfo<>();

        long total = couponUserPageInfo.getTotal();

        CouponUserVO couponUserVO = new CouponUserVO();

        couponUserVO.setTotal(total);
        couponUserVO.setItems(couponUsers);

        return couponUserVO;
    }

    @Override
    public Coupon readCoupon(Integer id) {

        Coupon coupon = couponMapper.selectByPrimaryKey(id);

        return coupon;
    }

    @Override
    public Coupon createCoupon(Coupon coupon) {

        coupon.setAddTime(new Date());

        coupon.setUpdateTime(new Date());

        coupon.setDeleted(false);

        couponMapper.insert(coupon);

        return  couponMapper.selectByPrimaryKey(coupon.getId());
    }

    @Override
    public void deleteCoupon(Coupon coupon) {

        coupon.setUpdateTime(new Date());

        coupon.setDeleted(true);

        couponMapper.updateByPrimaryKeySelective(coupon);
    }

    @Override
    public Coupon updateCoupon(Coupon coupon) {

        Coupon coupons = new Coupon();

        coupon.setUpdateTime(new Date());

        BeanUtils.copyProperties(coupon,coupons);

         couponMapper.updateByPrimaryKeySelective(coupons);

       return couponMapper.selectByPrimaryKey(coupons.getId());

    }
}
